package com.api.demo.test.business;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.api.demo.dto.business.order.*;
import com.api.demo.util.ApiSignUtil;
import com.api.demo.util.CommonUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 订单相关
 * @author: yanming
 * @createDate: 2022/12/26 14:27
 */
public class Order {


    /**
     * 查询任务类型-v2
     */
    public static String taskTypeListPath = "%s/api/order/task-type-v2";

    /**
     * 创建订单
     */
    public static String addOrderPath = "%s/api/order/add";

    /**
     * 校验订单
     */
    public static String validationOrderPath = "%s/api/order/validation";

    /**
     * 支付订单
     */
    public static String payPath = "%s/api/order/pay";

    /**
     * 废弃订单
     */
    public static String abandonOrderPath = "%s/api/order/api-order-abandon";

    /**
     * 关闭订单
     */
    public static String closeOrderPath = "%s/api/order/api-order-close";


    /**
     * 查询订单详情-v2
     */
    public static String getOrderInfo = "%s/api/order/v2";

    /**
     * 查询创客订单详情-v2
     */
    public static String getOrderWorkerInfo = "%s/api/order/worker-order-info-v2";
    /**
     * 上传任务交付物
     */
    public static String deliverables = "%s/api/order/api-deliverables";

    /**
     * 查询上传任务交付物-状态
     */
    public static String deliverablesStatus = "%s/api/order/api-deliverables-status";


    public static void main(String[] args) {

        /**
         * 查询任务类型-v2
         */
//        taskTypeList();


        /**
         * 创建订单
         */
//        addOrder();

        /**
         * 校验订单  需要已经存在的创客 不存在的创客会失败
         */
//        validationOrder();

        /**
         * 订单支付
         */
//        pay(7162174152835780608L);

        //创建和支付
//        createAndPayOrder();

        //废弃订单（创建的订单没有支付，调用这个接口，可以把订单变成 支付关闭状态）
//        abandonOrder(7162175791269584896L);

        //关闭支付（退服务费） 仅支持已经收了服务费的
//        closePay(7162180138003185664L);


        /**
         * 查询订单详情
         */
//        getOrderInfo();


        /**
         * 查询创客订单详情 （主要查询订单明细是否下发成功）
         */
//        getOrderWorkerInfo();

        //上传任务交付物
//        uploadDeliverables(7161881833707352064L);

        //查询任务交付物审核状态
        getDeliverablesStatus(1763045800474054657L);


    }


    /**
     * 查询任务类型-v2
     */
    public static void taskTypeList() {
        TaskTypeListDTO taskTypeListDTO = new TaskTypeListDTO();
        taskTypeListDTO.setAgentId(CommonUtil.AGENT_ID);
        taskTypeListDTO.setTimestamp(System.currentTimeMillis());
        taskTypeListDTO.setCreditCode(CommonUtil.creditCode);
        taskTypeListDTO.setTaxId(CommonUtil.TAX_ID);


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(5);
        signMap.put("agentId", taskTypeListDTO.getAgentId());
        signMap.put("timestamp", taskTypeListDTO.getTimestamp());
        signMap.put("taxId", taskTypeListDTO.getTaxId());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        taskTypeListDTO.setSign(sign);

        //请求地址
        String url = String.format(taskTypeListPath, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(taskTypeListDTO));
        System.out.println(post);

    }

    public static void createAndPayOrder() {
        String s = addOrder();
        JSONObject jsonObject = JSON.parseObject(s);
        Integer code = jsonObject.getInteger("code");
        if (200 != code) {
            return;
        }
        Long id = jsonObject.getLong("data");
        pay(id);
    }

    /**
     * 创建订单
     */
    public static String addOrder() {
        //订单信息
        AddOrUpdatePaymentOrderApiDTO addOrUpdatePaymentOrderApiDTO = new AddOrUpdatePaymentOrderApiDTO();
        addOrUpdatePaymentOrderApiDTO.setTimestamp(System.currentTimeMillis());

        //测试时 需换成自己对应的商户信息和代理商
        addOrUpdatePaymentOrderApiDTO.setAgentId(CommonUtil.AGENT_ID);
        addOrUpdatePaymentOrderApiDTO.setTaxId(CommonUtil.TAX_ID);
        addOrUpdatePaymentOrderApiDTO.setCreditCode(CommonUtil.creditCode);

        //非必填参数
        addOrUpdatePaymentOrderApiDTO.setNotifyUrl("www.baidu.com");
        //--------


        List<AddPaymentOrderWorkerDTO> addPaymentOrderWorkerDTOList = new ArrayList<>();


        //订单明细
        AddPaymentOrderWorkerDTO addPaymentOrderWorkerDTO = new AddPaymentOrderWorkerDTO();
        addPaymentOrderWorkerDTO.setWorkerName("颜明");
        addPaymentOrderWorkerDTO.setIdCardCode("430422199604277977");
//        addPaymentOrderWorkerDTO.setBankCode("6214851203618397");//正确的
        addPaymentOrderWorkerDTO.setBankCode("6214851203618398");//错误的
        addPaymentOrderWorkerDTO.setMobileCode("15099990637");
        addPaymentOrderWorkerDTO.setRealMoney(new BigDecimal("0.2"));

        //非必填参数
        addPaymentOrderWorkerDTO.setBankName("招商银行");
        //--------
        addPaymentOrderWorkerDTOList.add(addPaymentOrderWorkerDTO);


//        //订单明细
        AddPaymentOrderWorkerDTO addPaymentOrderWorkerDTO2 = new AddPaymentOrderWorkerDTO();
        addPaymentOrderWorkerDTO2.setWorkerName("任伟忠");
        addPaymentOrderWorkerDTO2.setIdCardCode("130722199208041714");
//        addPaymentOrderWorkerDTO2.setBankCode("6212260200148321074");//正确的
        addPaymentOrderWorkerDTO2.setBankCode("6212260200148321075");//错误的
        addPaymentOrderWorkerDTO2.setMobileCode("13691260678");
        addPaymentOrderWorkerDTO2.setRealMoney(new BigDecimal("0.2"));

        //非必填参数
        addPaymentOrderWorkerDTO2.setBankName("");
        //--------
        addPaymentOrderWorkerDTOList.add(addPaymentOrderWorkerDTO2);


        addOrUpdatePaymentOrderApiDTO.setWorkerDTOList(addPaymentOrderWorkerDTOList);
        //任务信息
        ApiAddTaskDTO apiAddTaskDTO = new ApiAddTaskDTO();
        apiAddTaskDTO.setTaskName("测,试任务");
        apiAddTaskDTO.setTaskDesc("为了测,试任务，创建一个测,试的任务");
//        apiAddTaskDTO.setTaskTypeId(1519593152447516673L);
        apiAddTaskDTO.setTaskTypeName("现代服务");
//        apiAddTaskDTO.setInvoiceCatalogId(1519226853611540481L);
        apiAddTaskDTO.setBillingCategory("*现代服务*企业形象策划");
        //min 小额   (默认都是这个)
        apiAddTaskDTO.setMaxMinType("min");

        //非必填参数
        apiAddTaskDTO.setTaskDesc("为了测,试任务，创建一个测,试的任务");
        //--------

        addOrUpdatePaymentOrderApiDTO.setAddTaskData(apiAddTaskDTO);


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(6);
        signMap.put("agentId", addOrUpdatePaymentOrderApiDTO.getAgentId());
        signMap.put("timestamp", addOrUpdatePaymentOrderApiDTO.getTimestamp());
        signMap.put("creditCode", addOrUpdatePaymentOrderApiDTO.getCreditCode());
        signMap.put("taxId", addOrUpdatePaymentOrderApiDTO.getTaxId());
        signMap.put("taskName", addOrUpdatePaymentOrderApiDTO.getAddTaskData().getTaskName());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        addOrUpdatePaymentOrderApiDTO.setSign(sign);

        //请求地址
        String url = String.format(addOrderPath, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(addOrUpdatePaymentOrderApiDTO));
        System.out.println(post);
        return post;
        //7007164004633313280
    }


    /**
     * 校验订单   需要已经存在的创客 不存在的创客会失败
     */
    public static void validationOrder() {
        //订单信息
        AddOrUpdatePaymentOrderApiDTO addOrUpdatePaymentOrderApiDTO = new AddOrUpdatePaymentOrderApiDTO();
        addOrUpdatePaymentOrderApiDTO.setTimestamp(System.currentTimeMillis());

        //测试时 需换成自己对应的商户信息和代理商
        addOrUpdatePaymentOrderApiDTO.setAgentId(CommonUtil.AGENT_ID);
        addOrUpdatePaymentOrderApiDTO.setTaxId(CommonUtil.TAX_ID);
        addOrUpdatePaymentOrderApiDTO.setCreditCode(CommonUtil.creditCode);

        //非必填参数
        addOrUpdatePaymentOrderApiDTO.setNotifyUrl("www.baidu.com");
        //--------


        //订单明细
        AddPaymentOrderWorkerDTO addPaymentOrderWorkerDTO = new AddPaymentOrderWorkerDTO();
        addPaymentOrderWorkerDTO.setWorkerName("颜明");
        addPaymentOrderWorkerDTO.setIdCardCode("430422199604277977");
        addPaymentOrderWorkerDTO.setBankCode("6214851203618397");
        addPaymentOrderWorkerDTO.setMobileCode("15099990637");
        addPaymentOrderWorkerDTO.setRealMoney(new BigDecimal("0.2"));

        //非必填参数
        addPaymentOrderWorkerDTO.setBankName("招商银行");
        //--------

        List<AddPaymentOrderWorkerDTO> addPaymentOrderWorkerDTOList = new ArrayList<>();
        addPaymentOrderWorkerDTOList.add(addPaymentOrderWorkerDTO);
        addOrUpdatePaymentOrderApiDTO.setWorkerDTOList(addPaymentOrderWorkerDTOList);

        //任务信息
        ApiAddTaskDTO apiAddTaskDTO = new ApiAddTaskDTO();
        apiAddTaskDTO.setTaskName("测,试任务");
        apiAddTaskDTO.setTaskDesc("为了测,试任务，创建一个测,试的任务");
//        apiAddTaskDTO.setTaskTypeId(1519593152447516673L);
        apiAddTaskDTO.setTaskTypeName("现代服务");
//        apiAddTaskDTO.setInvoiceCatalogId(1519226853611540481L);
        apiAddTaskDTO.setBillingCategory("*现代服务*企业形象策划");
        //min 小额   (默认都是这个)
        apiAddTaskDTO.setMaxMinType("min");

        //非必填参数
        apiAddTaskDTO.setTaskDesc("为了测,试任务，创建一个测,试的任务");
        //--------

        addOrUpdatePaymentOrderApiDTO.setAddTaskData(apiAddTaskDTO);


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(6);
        signMap.put("agentId", addOrUpdatePaymentOrderApiDTO.getAgentId());
        signMap.put("timestamp", addOrUpdatePaymentOrderApiDTO.getTimestamp());
        signMap.put("creditCode", addOrUpdatePaymentOrderApiDTO.getCreditCode());
        signMap.put("taxId", addOrUpdatePaymentOrderApiDTO.getTaxId());
        signMap.put("taskName", addOrUpdatePaymentOrderApiDTO.getAddTaskData().getTaskName());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        addOrUpdatePaymentOrderApiDTO.setSign(sign);

        //请求地址
        String url = String.format(validationOrderPath, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(addOrUpdatePaymentOrderApiDTO));
        System.out.println(post);

        //7007164004633313280
    }


    /**
     * 订单支付
     */
    public static void pay(Long id) {
        ApiOrderPayDTO apiOrderPayDTO = new ApiOrderPayDTO();
        apiOrderPayDTO.setAgentId(CommonUtil.AGENT_ID);
        apiOrderPayDTO.setId(id);
        apiOrderPayDTO.setTimestamp(System.currentTimeMillis());
        apiOrderPayDTO.setPaymentMethod(PaymentMethod.ZSBK);


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(4);
        signMap.put("agentId", apiOrderPayDTO.getAgentId());
        signMap.put("timestamp", apiOrderPayDTO.getTimestamp());
        signMap.put("id", apiOrderPayDTO.getId());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        apiOrderPayDTO.setSign(sign);

        //请求地址
        String url = String.format(payPath, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(apiOrderPayDTO));
        System.out.println(post);

    }

    //关闭支付（退服务费） 仅支持已经收了服务费的
    private static void closePay(Long id) {
        OrderInfoDTO orderInfoDTO = new OrderInfoDTO();
        orderInfoDTO.setId(id);
        orderInfoDTO.setAgentId(CommonUtil.AGENT_ID);
        orderInfoDTO.setTimestamp(System.currentTimeMillis());


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(4);
        signMap.put("agentId", orderInfoDTO.getAgentId());
        signMap.put("timestamp", orderInfoDTO.getTimestamp());
        signMap.put("id", orderInfoDTO.getId());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        orderInfoDTO.setSign(sign);

        //请求地址
        String url = String.format(closeOrderPath, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(orderInfoDTO));
        System.out.println(post);
    }


    /**
     * 废弃订单（创建的订单没有支付，调用这个接口，可以把订单变成 支付关闭状态）
     *
     * @param id
     */
    private static void abandonOrder(Long id) {
        OrderInfoDTO orderInfoDTO = new OrderInfoDTO();
        orderInfoDTO.setId(id);
        orderInfoDTO.setAgentId(CommonUtil.AGENT_ID);
        orderInfoDTO.setTimestamp(System.currentTimeMillis());


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(4);
        signMap.put("agentId", orderInfoDTO.getAgentId());
        signMap.put("timestamp", orderInfoDTO.getTimestamp());
        signMap.put("id", orderInfoDTO.getId());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        orderInfoDTO.setSign(sign);

        //请求地址
        String url = String.format(abandonOrderPath, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(orderInfoDTO));
        System.out.println(post);
    }


    /**
     * 查询订单详情
     */
    private static void getOrderInfo() {

        OrderInfoDTO orderInfoDTO = new OrderInfoDTO();
        orderInfoDTO.setId(7161881833707352064L);
        orderInfoDTO.setAgentId(CommonUtil.AGENT_ID);
        orderInfoDTO.setTimestamp(System.currentTimeMillis());


        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(4);
        signMap.put("agentId", orderInfoDTO.getAgentId());
        signMap.put("timestamp", orderInfoDTO.getTimestamp());
        signMap.put("id", orderInfoDTO.getId());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        orderInfoDTO.setSign(sign);

        //请求地址
        String url = String.format(getOrderInfo, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(orderInfoDTO));
        System.out.println(post);
    }


    /**
     * 查询创客订单详情
     */
    public static void getOrderWorkerInfo() {
        WorkerOrderInfoDTO workerOrderInfoDTO = new WorkerOrderInfoDTO();
        workerOrderInfoDTO.setAgentId(CommonUtil.AGENT_ID);
        workerOrderInfoDTO.setTimestamp(System.currentTimeMillis());
        workerOrderInfoDTO.setCreditCode(CommonUtil.creditCode);

        //非必填参数
//        workerOrderInfoDTO.setOrderId(7007164004633313280L);
//        workerOrderInfoDTO.setIdCardCode("430422199604277977");
//        workerOrderInfoDTO.setName("颜明");
//
//        workerOrderInfoDTO.setStartDate("2022-12-28 00:00:00");
//        workerOrderInfoDTO.setEndDate("2022-12-29 00:00:00");
//        workerOrderInfoDTO.setSort("y");
        //--------

        //参与加密的参数    固定的几个字段缺一不可
        Map<String, Object> signMap = new HashMap<>(4);
        signMap.put("agentId", workerOrderInfoDTO.getAgentId());
        signMap.put("timestamp", workerOrderInfoDTO.getTimestamp());
        signMap.put("creditCode", workerOrderInfoDTO.getCreditCode());
        signMap.put("key", CommonUtil.KEY);
        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        workerOrderInfoDTO.setSign(sign);

        //请求地址
        String url = String.format(getOrderWorkerInfo, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(workerOrderInfoDTO));
        System.out.println(post);


    }


    /**
     * 上传任务交付物
     */
    public static void uploadDeliverables(Long id) {

        DeliverablesDTO deliverablesDTO = new DeliverablesDTO();
        deliverablesDTO.setAgentId(CommonUtil.AGENT_ID);
        deliverablesDTO.setTimestamp(System.currentTimeMillis());

        deliverablesDTO.setDeliverablesDesc("任务交付物描述");
        deliverablesDTO.setOrderId(id);
        deliverablesDTO.setFileSuffix("pdf"); //文件格式
        deliverablesDTO.setFileBase64(false);
        deliverablesDTO.setFile(CommonUtil.commonImageUrl);

        Map<String, Object> signMap = JSON.parseObject(JSON.toJSONString(deliverablesDTO), new TypeReference<Map<String, Object>>() {
        });
        signMap.put("key", CommonUtil.KEY);

        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        deliverablesDTO.setSign(sign);


        //请求地址
        String url = String.format(deliverables, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(deliverablesDTO));
        System.out.println(post);


    }

    /**
     * 查询任务交付物审核状态
     *
     * @param id
     */
    private static void getDeliverablesStatus(Long id) {

        SelectDeliverablesStatusDTO selectDeliverablesStatusDTO = new SelectDeliverablesStatusDTO();
        selectDeliverablesStatusDTO.setAgentId(CommonUtil.AGENT_ID);
        selectDeliverablesStatusDTO.setTimestamp(System.currentTimeMillis());

        selectDeliverablesStatusDTO.setId(id);

        Map<String, Object> signMap = JSON.parseObject(JSON.toJSONString(selectDeliverablesStatusDTO), new TypeReference<Map<String, Object>>() {
        });
        signMap.put("key", CommonUtil.KEY);

        String sign = ApiSignUtil.getSign(JSON.parseObject(JSON.toJSONString(signMap)));
        System.out.println(sign);
        selectDeliverablesStatusDTO.setSign(sign);


        //请求地址
        String url = String.format(deliverablesStatus, CommonUtil.REQUEST_URL);
        System.out.println("请求地址： " + url);
        String post = HttpUtil.post(url, JSON.toJSONString(selectDeliverablesStatusDTO));
        System.out.println(post);
    }


}